﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bymed.SpineScan.Service.Shared
{
    public enum DataSourceType
    {
        SqlClient,
        SqlServerCe,
        Unkown
    }

    public static class ConfigFileUtility
    {
        public const string AppSettingsSectionName = "appSettings";
        private static Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        private static AppSettingsSection _appSettingsSection = _config.AppSettings;

        /// <summary>
        /// Get value associated with key in config file
        /// </summary>
        /// <param name="key">Key in appSettings</param>
        /// <returns>Value associated with parameter key</returns>
        public static object GetValue(string key)
        {
            return _appSettingsSection.Settings[key].Value;
        }

        /// <summary>
        /// Change value associated with key in config file
        /// </summary>
        /// <param name="key">Key in appSettings</param>
        /// <param name="value">The new value</param>
        public static void ChangeValue(string key, string value)
        {
            _appSettingsSection.Settings[key].Value = value;
            _config.Save(ConfigurationSaveMode.Modified);
        }

        /// <summary>
        /// Refreshes appSettings so the next time that it is retrieved it will be re-read from disk.
        /// </summary>
        public static void RefreshAppSettingsSection()
        {
            ConfigurationManager.RefreshSection(AppSettingsSectionName);
        }

        /// <summary>
        /// Get the data source type from the connection string in config file.
        /// </summary>
        /// <param name="connectionName">The connection section name in config file.</param>
        /// <returns>DataSourceType</returns>
        public static DataSourceType GetDataSourceType(string connectionName)
        {
            var providerName = ConfigurationManager.ConnectionStrings[connectionName].ProviderName.ToLower();

            if (providerName.Contains("sqlclient"))
            {
                return DataSourceType.SqlClient;
            }
            else if (providerName.Contains("sqlserverce"))
            {
                return DataSourceType.SqlServerCe;
            }

            return DataSourceType.Unkown;
        }

        /// <summary>
        /// Get the Sql Compact data file full name.
        /// </summary>
        /// <param name="connectionName">The connection section name in config file.</param>
        /// <returns>The Sql Compact data file full name. If the data source is not matched, then null will be returned.</returns>
        public static string GetDataSourceFileFullName(string connectionName)
        {
            if (GetDataSourceType(connectionName) == DataSourceType.SqlServerCe)
            {
                var connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
                var dataSource = connectionString.Substring(12).TrimEnd(';');
                var dataSourceFileFullName = new FileInfo(Path.Combine(Environment.CurrentDirectory, dataSource)).FullName;

                return File.Exists(dataSourceFileFullName) ? dataSourceFileFullName : null;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Get the data source file export path full name.
        /// </summary>
        /// <returns>The data source file export path full name.</returns>
        public static string GetDataSourceFileExportPathFullName()
        {
            var dataSourceExportDirectoryName = GetValue(ConfigurationConstant.DataSourceExportDirectoryName) as string;
            var dataSourceExportDirectoryFullName = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, dataSourceExportDirectoryName)).FullName;

            if (!Directory.Exists(dataSourceExportDirectoryFullName))
            {
                Directory.CreateDirectory(dataSourceExportDirectoryFullName);
            }

            return dataSourceExportDirectoryFullName;
        }

        /// <summary>
        /// Get the patient data xml file export path full name.
        /// </summary>
        /// <returns>The patient data xml file export path full name.</returns>
        public static string GetPatientDataExportPathFullName()
        {
            var patientDataExportDirectoryName = GetValue(ConfigurationConstant.PatientDataExportDirectoryName) as string;
            var patientDataExportDirectoryFullName = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, patientDataExportDirectoryName)).FullName;

            if (!Directory.Exists(patientDataExportDirectoryFullName))
            {
                Directory.CreateDirectory(patientDataExportDirectoryFullName);
            }

            return patientDataExportDirectoryFullName;
        }
    }
}
